// pages/article/classDetail/index.js
const {
  checkLogin
} = require('../../../utils/util')
const {
  couponUseList,
  reviewPay,
  getDictData,
  volunteerPay,
  coursePay
} = require('../../../api/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    details: {},
    bottomLift: getApp().globalData.bottomLift,
    loginVisible: true,
    visible: false,
    title: '可用优惠券',
    content: '你有一张优惠券可用',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.params) {
      const params = JSON.parse(decodeURIComponent(options.params))
      console.log(params.content)
      if (params.json) {
        this.setData({
          payObj: JSON.parse(params.json)
        })
      }
      this.setData({
        details: params
      })
      wx.setNavigationBarTitle({
        title: params.title,
      })
    }
  },
  confirm(e) {
    const couponId = e.detail
    this.doWxPay(couponId)
  },
  async pay() {

    const {
      payObj
    } = this.data
    if (payObj.type == 'test') {
      this.review()
    } else {
      const {
        result
      } = await couponUseList(payObj.price)
      if (result.length > 0) {
        this.setData({
          visible: true,
          couponList: result,
          content: '您有' + result.length + '张优惠券可用'
        })
      } else {
        this.doWxPay()
      }
    }

  },

  async doWxPay(couponId) {
    const {
      payObj
    } = this.data
    if (payObj.type == 'service') {
      const params = {
        volunteerName: payObj.title,
        price: payObj.price
      }
      if (couponId) {
        params.couponId = couponId
      }
      const {
        result
      } = await volunteerPay(params)
      const _this = this
      wx.requestPayment({
        nonceStr: result.nonceStr,
        package: result.package,
        paySign: result.paySign,
        timeStamp: result.timeStamp,
        signType: result.signType,
        success: async function (res) {
          wx.showToast({
            icon: 'success',
            title: '购买成功',
          })
        },
        fail: function (res) {

        }
      })
    } else if (payObj.type == 'course') {
      const params = {}
      if (couponId) {
        params.couponId = couponId
      }
      const {
        result
      } = await coursePay(params)
      const _this = this
      wx.requestPayment({
        nonceStr: result.nonceStr,
        package: result.package,
        paySign: result.paySign,
        timeStamp: result.timeStamp,
        signType: result.signType,
        success: async function (res) {
          wx.showToast({
            icon: 'success',
            title: '购买成功',
          })
        },
        fail: function (res) {}
      })
    }

  },

  review() {
    reviewPay().then((result) => {
      wx.requestPayment({
        nonceStr: result.result.nonceStr,
        package: result.result.package,
        paySign: result.result.paySign,
        timeStamp: result.result.timeStamp,
        signType: result.result.signType,
        success: async function () {
          wx.showToast({
            icon: 'success',
            title: '支付完成',
          })
          wx.switchTab({
            url: '/pages/test/index/index',
          })
        },
        fail: function () {}
      })
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    const {details}=this.data
    return {
      title: '德道升学',
      imageUrl:'https://ehsimg.oss-cn-beijing.aliyuncs.com/images/static/users/shareImg.jpg',
      path:`/pages/article/class/index?params=`+encodeURIComponent(JSON.stringify(details))
    }
  }
})